package faceService;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.qcloud.image.ImageClient;
import com.qcloud.image.common_utils.CommonFileUtils;
import com.qcloud.image.demo.Demo;
import com.qcloud.image.request.FaceDetectRequest;
import dto.TecentDetectDto;
import util.AppContast;

import java.util.logging.Level;
import java.util.logging.Logger;

import static com.qcloud.image.http.RequestBodyKey.BUCKET;

/**
 * Created by Administrator on 2017/9/29.
 */
public class FaceDetect {
    public TecentDetectDto faceDetect(String imgpath) {
        ImageClient imageClient = new ImageClient(AppContast.APP_ID, AppContast.SERCET_ID, AppContast.SERCET_KEY);
        String bucketName = BUCKET;

        String faceDetectName  = "";
        String faceDetectImage = "";
        try {
            faceDetectName = "face1.jpg";
            faceDetectImage = CommonFileUtils.getFileContent(imgpath);
        } catch (Exception ex) {
            Logger.getLogger(Demo.class.getName()).log(Level.SEVERE, null, ex);
        }

        FaceDetectRequest faceDetectReq = new FaceDetectRequest(bucketName, faceDetectName, faceDetectImage, 0);
        String ret = imageClient.faceDetect(faceDetectReq);

        JSONObject jsonObject = JSON.parseObject(ret);

        TecentDetectDto tecentDetectDto = JSON.parseObject(ret, new TypeReference<TecentDetectDto>() {});

//        System.out.println("face detect ret:" + ret);
        return tecentDetectDto;
    }


}
